package com.jc.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jc.order.common.OrderNumberGenerator;
import com.jc.order.domain.KjbInsuranceTraditional;
import com.jc.order.dto.JcPolicyDTO;
import com.jc.order.mapper.KjbInsuranceTraditionalMapper;
import com.jc.order.service.InsuranceTraditionalService;
import com.jc.order.service.KjbInsurancesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class InsuranceTraditionalServiceImpl implements InsuranceTraditionalService {

    @Resource
    private KjbInsuranceTraditionalMapper kjbInsuranceTraditionalMapper;
    @Resource
    private KjbInsurancesService kjbInsurancesService;


    @Override
    @Transactional
    public JcPolicyDTO insertPolicy(JcPolicyDTO dto) {
        KjbInsuranceTraditional entity = new KjbInsuranceTraditional();
        dto.setSnNo(OrderNumberGenerator.generate());
        BeanUtils.copyProperties(dto, entity);
        entity.setInsuranceSn("");
        kjbInsuranceTraditionalMapper.insert(entity);
        dto.setId(entity.getId());
        kjbInsurancesService.saveInsurances(dto);
        return dto;
    }

    @Override
    @Transactional
    public JcPolicyDTO updatePolicy(JcPolicyDTO dto) {
        KjbInsuranceTraditional entity = new KjbInsuranceTraditional();
//        dto.setSnNo(OrderNumberGenerator.generate());
        BeanUtils.copyProperties(dto, entity);
        kjbInsuranceTraditionalMapper.updateByPrimaryKey(entity);
//        dto.setId(entity.getId());
        kjbInsurancesService.saveInsurances(dto);
        return dto;
    }

    @Override
    public JcPolicyDTO queryById(Long id) {

        JcPolicyDTO dto = null;
        KjbInsuranceTraditional entity = kjbInsuranceTraditionalMapper.selectByPrimaryKey(id);
        if (entity != null) {
            dto = new JcPolicyDTO();
            BeanUtils.copyProperties(dto, entity);
        }
        return dto;
    }

    @Override
    public JcPolicyDTO selectByOrderNumber(String orderNumber, Long tenantId) {
        JcPolicyDTO dto = null;
        Example example = new Example(KjbInsuranceTraditional.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andEqualTo("orderNumber", orderNumber).andEqualTo("tenantId", tenantId);
        List<KjbInsuranceTraditional> traditionalList = kjbInsuranceTraditionalMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            dto = new JcPolicyDTO();
            BeanUtils.copyProperties(traditionalList.stream().findFirst(), dto);
        }
        return dto;
    }

    @Override
    public JcPolicyDTO selectByOrderNumberAndSnNo(String orderNumber, String snNo, Long tenantId) {
        JcPolicyDTO dto = null;
        Example example = new Example(KjbInsuranceTraditional.class, Boolean.TRUE, Boolean.TRUE);
        if (StrUtil.isNotBlank(orderNumber)) {
            example.createCriteria().andEqualTo("orderNumber", orderNumber);
        }
        example.createCriteria().andEqualTo("snNo", snNo).andEqualTo("tenantId", tenantId);
        List<KjbInsuranceTraditional> traditionalList = kjbInsuranceTraditionalMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            dto = new JcPolicyDTO();
//            BeanUtils.copyProperties(traditionalList.stream().findFirst(), dto);
            BeanUtils.copyProperties(traditionalList.get(0), dto);
        }
        return dto;
    }

    @Override
    public List<JcPolicyDTO> selectList(List<String> snNos, Long tenantId) {

        if (tenantId == null || snNos == null || snNos.size() == 0) {
            return null;
        }
        List<JcPolicyDTO> list = null;
        Example example = new Example(KjbInsuranceTraditional.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andIn("snNo", snNos).andEqualTo("tenantId", tenantId);
        List<KjbInsuranceTraditional> traditionalList = kjbInsuranceTraditionalMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            list = new ArrayList<>();
            for (int i = 0; i < traditionalList.size(); i++) {
                JcPolicyDTO dto = new JcPolicyDTO();
                KjbInsuranceTraditional entity = traditionalList.get(i);
                BeanUtils.copyProperties(entity, dto);
                dto.setRmbPremium(dto.getFee());
                list.add(dto);
            }
        }
        return list;
    }
}
